********** PS Special Issue on Measuring Democratic Backsliding **********
*********** Adapting McMann et al. 2022 Table 3 for Appendix B ***********
******* This replication file by Brigitte Seim, seimbri@gmail.com ********


/*** PREPARED BY:
===================================================
Brigitte Seim
Associate Professor, Department of Public Policy
The University of North Carolina at Chapel Hill
V-Dem Project Manager of Experiments
+1 (650) 492-0053
seimbri@gmail.com
Prepared in Stata 14
===================================================
***/

** SETTINGS **

* Disable "more" command
set more off

* Set memory
set matsize 400

* Set working directory - ***CHANGE CODE HERE TO FOLDER WITH FILES***
cd "/Users/bas/Library/Mobile Documents/com~apple~CloudDocs/Research/Projects/V-Dem/PS Special Issue"

* Bring in coder dataset
use "coder_level_ds_v13.dta", clear 

** PREPARING SD MEASURES **

collapse (sd) sd_v2elfrefair=v2elfrfair sd_v2elboycot=v2elboycot sd_v2elfrefair_beta=v2elfrfair_beta sd_v2elboycot_beta=v2elboycot_beta (count) v2elfrfair v2elboycot, by(country_id historical_date)

gen year=substr(historical_date, 1, 4)
gen month=substr(historical_date, 6, 2)
gen day=substr(historical_date, 9, 2)
destring year, replace
destring month, replace
destring day, replace
gen date=mdy(month, day, year)
format date %td
drop historical_date
rename date historical_date
merge m:m country_id historical_date using "V-Dem-CD-v13"
drop if _merge==2
rename v2x_freexp freexp
gen century=year/100

eststo clear
eststo boycot: reg sd_v2elboycot century freexp c.v2x_polyarchy##c.v2x_polyarchy v2elboycot, cluster(country_id)
eststo frefair: reg sd_v2elfrefair century freexp c.v2x_polyarchy##c.v2x_polyarchy v2elfrfair, cluster(country_id)
sum sd_v2elboycot
sum sd_v2elfrefair

* clean up before writing tables to file
label var century "Century"
label var v2x_polyarchy "Level of Democracy"
label var v2elboycot "Number of Coders"
label var v2elfrfair "Number of Coders"
label var freexp "Freedom of Expression"

* print to files
* Table
esttab boycot frefair using AppendixB.tex, b(3) se(3) ///
  star(* 0.10 ** 0.05 *** 0.01) sca("r2_a" "N_clust" "N") replace ///
  keep(century freexp v2x_polyarchy c.v2x_polyarchy#c.v2x_polyarchy v2elboycot v2elfrfair) obslast label nogaps ///
  nodepvars mtitles("v2elboycot" "v2elfrefair") interaction (" x ") nonumbers nonotes ///
  title ("Predicting Respondent Disagreement") ///
  addnotes("Entries are regression coefficients, with standard errors, clustered on countries, in parentheses. * p < 0.10, ** p < 0.05, *** p < 0.01. This analysis was produced with v13 of the V-Dem dataset.")


